<?php

namespace App\Http\Listeners\Admin;

use App\Http\Events\Admin\AdminEvent;
use App\Http\Model\Admin\User;
use App\Http\Model\Admin\UserActionLog;
use Carbon\Carbon;
use Config;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Crypt;
use Monolog\Logger;

class AdminListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }


    /**
     * Handle the event.
     *
     * @param  AdminEvent $event
     * @return void
     */
    public function handle(AdminEvent $event)
    {
        $user = session('dmp_user');
        $event_type = $event->event_type;
        $new_obj = $event->new_obj;
        $old_obj = $event->old_obj;

        $input = [
            'user_id' => $user['id'],
            'user_name' => $user['name'],
            'ip' => $user['last_login_ip'],
            'action_name' => Config::get('admin.templates.' . $event_type . '.name'),
            'content' => $this->getContentStr($event_type, $user, $new_obj, $old_obj)
        ];
        if($input['content']) {
            $action_ins = new UserActionLog();
            $action_ins->createNew($input);
            if ($action_ins) {

            }
        }
    }
    /*
     * 获取content 模板
     *
     * */
    public function getContentStr($event_type, $user, $new_obj, $old_obj)
    {
        $info = '';
        if (!$event_type) {
            return '';
        }
        $mark_tpl = Config::get('admin.templates.' . $event_type . '.mark_tpl');
        $tip = Config::get('admin.templates.' . $event_type . '.tips');

        if($event_type == 'set_role_menu'){
            $info = preg_replace(['/{user_name}/', '/{target_id}/','/{target_name}/'], [$user['name'], $old_obj && $old_obj['role_id']? $old_obj['role_id'] : '',$old_obj && $old_obj['name'] ? $old_obj['name'] : ''], $mark_tpl);
            $info .= preg_replace(['/{new_value}/'], json_encode($new_obj), $tip);
        }
        else if($event_type == 'add_tourist_action'){
            $info = preg_replace(['/{user_name}/'], [$user['name']], $mark_tpl);
            $info .= preg_replace(['/{ids}/'], json_encode($new_obj), $tip);
        }
        else {
            if ($tip) {
                $tip_info = '';
                foreach ($new_obj as $key => $item) {
                    if ($key == 'password') {
                        if (Crypt::decrypt($new_obj[$key]) != Crypt::decrypt($old_obj[$key])) {
                            $tip_info .= preg_replace(['/{key}/', '/{old_value}/', '/{new_value}/'], [$key, '**', '**'], $tip);
                        }
                    } else {
                        if(is_array($item)){
                            if(array_diff($item,$old_obj[$key])) {
                                $tip_info .= preg_replace(['/{key}/', '/{old_value}/', '/{new_value}/'], [$key, json_encode($old_obj[$key]), json_encode($new_obj[$key])], $tip);
                            }
                        }
                        else {
                            if ($old_obj[$key] != $item) {
                                $tip_info .= preg_replace(['/{key}/', '/{old_value}/', '/{new_value}/'], [$key, $old_obj[$key], $new_obj[$key]], $tip);
                            }
                        }
                    }
                }
                if (strlen($tip_info)) {
                    $info = preg_replace(['/{user_name}/', '/{target_id}/','/{target_name}/'], [$user['name'], $old_obj['id'],$old_obj['name']], $mark_tpl) . $tip_info;
                }
            } else {
                $info = preg_replace(['/{user_name}/', '/{target_id}/','/{new_name}/'], [$user['name'], $new_obj && $new_obj['id'] ? $new_obj['id'] : '',$new_obj && $new_obj['name'] ? $new_obj['name'] : ''], $mark_tpl);
            }
        }
        return $info;
    }
}
